package com.homer.gps.track.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.homer.gps.track.R;
import com.homer.gps.track.helper.UsefullStaff;
import com.homer.gps.track.persist.PlaceModel;

public class PlacesArrayAdapter extends ArrayAdapter<PlaceModel>{
	
	private final String LOG_TAG = "PlacesAdapter";

	private List<PlaceModel> places = null;
	private Activity context = null;
	private boolean deleteMode = false;
	
	public PlacesArrayAdapter(final Activity context, final int textViewResourceId, final List<PlaceModel> places) {
		super(context, textViewResourceId, places);
		this.context = context;
		this.places = places;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			rowView = inflater.inflate(R.layout.places_list_item, parent, false);
		}
		
		TextView placeName = (TextView) rowView.findViewById(R.id.place_name);
		TextView placeAddress = (TextView) rowView.findViewById(R.id.place_address);
		ImageView typePicture = (ImageView) rowView.findViewById(R.id.place_type_icon);
		PlaceModel place = (PlaceModel)places.get(position);
		
		placeName.setText(String.valueOf(place.getName()));
		String addr = (String) place.getParsedAddress();
		if (addr.equals("")) {
			placeAddress.setText(R.string.no_value);
		}else{
			placeAddress.setText(addr);
		}
		
		ImageView deleteBt = (ImageView )rowView.findViewById(R.id.delete_bt);
		deleteBt.setTag(place);
		if (deleteMode) {
			deleteBt.setVisibility(View.VISIBLE);
		} else {
			deleteBt.setVisibility(View.INVISIBLE);
		}
		
		typePicture.setImageResource(UsefullStaff.getPlaceTypeImageResource(place.getType()));
 
		return rowView;
	}
	
	public void setDeleteMode(boolean value) {
		deleteMode = value;
		notifyDataSetChanged();	
	}
}
